﻿using AspDotNet.Framework.Mvc.FirstDemo.Models;
using AspDotNet.Framework.Mvc.FirstDemo.Tools.Extension;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace AspDotNet.Framework.Mvc.FirstDemo.Controllers
{
    public class StudentController : BaseController
    {
        // GET: Student/SelectStudent/1
        public ActionResult SelectStudent(int? id)
        {
            ViewBag.Student = Student.Students.Find(p => p.ID == id);
            return View();
        }

        // GET: Student/ShowStudents
        public ActionResult ShowStudents()
        {
            ViewBag.StudentList = Student.Students;
            return View();
        }

        public override void Index()
        {
            base.Index();
            Response.Write($"<div><a href='{Request.Url.Scheme}://{Request.Url.Host}:{Request.Url.Port}/{Request.RequestContext.RouteData.Values.First().Value}/Find' target='_blank'>Find</a></div>");
            Response.Write($"<div><a href='{Request.Url.Scheme}://{Request.Url.Host}:{Request.Url.Port}/{Request.RequestContext.RouteData.Values.First().Value}/GetStudent/1?used-NonAction-attribute' target='_blank'>GetStudent</a></div>");
        }

        [ActionName("Find")]
        public string F()
        {
            return "This is Index action method of StudentController";
        }

        [NonAction]
        public Student GetStudent(int id)
        {
            return Student.Students.Where(s => s.ID == id).FirstOrDefault();
        }

        public ActionResult EditFromFormCollection(FormCollection form)
        {
            string json = JsonConvert.SerializeObject(form.GetAllData());
            return View(JsonConvert.DeserializeObject<Student>(json));
        }

        public ActionResult EditByBind(
            [Bind(Include = "Name,Birthday,FavouriteMusic,ID", Exclude = "IsChecked")] Student student)
        {
            return View(student);
        }

        public ActionResult Edit(Student student)
        {
            return View(student);
        }
    }
}